package tsi

import (
	watchmessgae "earth/watch_tcp/watch_message"
	"errors"
	"strconv"

	"github.com/sirupsen/logrus"
)

/*
上传血氧数据
终端发送[DW*YYYYYYYYYYYYYYY*LEN*oxygen,x]
x 表示血氧饱和度
示例[DW*334588000000156*0009*oxygen,15]
平台回复：
[DW*334588000000156*0006*oxygen]
*/

// 上传血氧数据
type HealthDataOxygen struct {
	Oxygen int // 血氧饱和度
}

func DealHealthDataOxygen(watchMessage *watchmessgae.WatchMessage, deviceID string) error {
	// 校验消息长度
	if len(watchMessage.MessageContentList) < 1 {
		logrus.Error("message is too short:", watchMessage.MessageContentList)
		return errors.New("message is too short")
	}
	// 解析消息内容
	healthDataOxygen := &HealthDataOxygen{}
	oxygen, err := strconv.Atoi(watchMessage.MessageContentList[0])
	if err != nil {
		return err
	}
	healthDataOxygen.Oxygen = oxygen
	return SendEvent(deviceID, healthDataOxygen, "temp")
}
